﻿CREATE PROCEDURE [dbo].[proc_Order_Change_Getlist_my]
	(
		@CompanyId int,
		@UserName nvarchar(50),
		@flag int,
		@ChangeType int,
		@Stext nvarchar(50),
		@StartDate nvarchar(10),
		@EndDate nvarchar(10),
		@StartIndex int,
		@EndIndex int
	)
AS
Begin
	Declare @sRq Datetime,@eRq Datetime
	Set @sRq = Convert(Datetime,@StartDate+' 00:00:00')
	Set @eRq = Convert(Datetime,@EndDate+' 23:59:59')

	;WITH list As(Select ROW_NUMBER() OVER (ORDER BY shqrq Desc,a.Id DESC)AS Row
		,a.Id
		,a.OId
		,a.CusId
		,a.CusName
		,shqr
		,shqrxm
		,shqrq
		,bgnr
		,oldje
		,newje
		,a.flag
,IsNull(b.ShiShouPrice,0) As ShiShouPrice
,a.CompanyId
,a.ChangeType
,b.NOE_Flag
--,Isnull((select Pizhu from Customer_PiZhu where CustomerId=a.CusId),'') as Pizhu --批注内容字段 2015.08.20 
--		,Isnull((select CusState from Customer_PiZhu where CustomerId=a.CusId),0) as CusState --是否有批注字段 
		From [Order_Change] a,[Order] b
		Where shqr=@UserName
		And (@flag=100 Or a.flag=@flag)
		And (@ChangeType=100 Or a.ChangeType=@ChangeType)
		And shqrq>=@sRq And shqrq<=@eRq
		And (@Stext='' Or a.CusName like '%'+@Stext+'%')
		And a.OId=b.OId
	)
	
	Select *,(Select Count(0) From list) As RecordCount From list
	Where Row Between @StartIndex and @EndIndex Order By Row
End
